package com.ytjj.qmyx.mall.service.impl;

import com.ytjj.common.utils.TokenUtil;
import com.ytjj.qmyx.mall.mapper.UsersGetuiInfoMapper;
import com.ytjj.qmyx.mall.model.UsersGetuiInfo;
import com.ytjj.qmyx.mall.model.UsersGetuiInfoExample;
import com.ytjj.qmyx.mall.service.UsersGetuiInfoService;
import com.ytjj.qmyx.users.mapper.UsersMapper;
import com.ytjj.qmyx.users.model.Users;
import com.ytjj.qmyx.users.model.UsersExample;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class UsersGetuiInfoServiceImpl implements UsersGetuiInfoService {
    @Resource
    private UsersGetuiInfoMapper usersGetuiInfoMapper;
    @Resource
    private UsersMapper usersMapper;

    @Override
    public int insert(UsersGetuiInfo usersGetuiInfo) {
        return usersGetuiInfoMapper.insert(usersGetuiInfo);
    }

    @Override
    public int update(UsersGetuiInfo usersGetuiInfo) {
        return usersGetuiInfoMapper.updateByPrimaryKey(usersGetuiInfo);
    }

    @Override
    public UsersGetuiInfo getByUserId(Integer userId) {
        UsersGetuiInfoExample example = new UsersGetuiInfoExample();
        example.createCriteria().andUserIdEqualTo(userId);
        example.setOrderByClause("create_time desc");
        List<UsersGetuiInfo> list = usersGetuiInfoMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    @Async
    @Override
    public void updateUserVisitId(Integer userId,String visitorId){
        if(null == userId){
            return;
        }
        if(StringUtils.isNotEmpty(visitorId)){
            log.info("========正在更新用户浏览器的唯一标识！====");
            Users users = new Users();
            users.setVisitorId(visitorId);
            //更新条件
            UsersExample usersExample = new UsersExample();
            usersExample.createCriteria().andIdEqualTo(userId);
            usersMapper.updateByExampleSelective(users,usersExample);
        }
    }
}
